package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.PlatformTodoStatisticsMapper;
import com.hq.ims.data.entity.PlatformTodoStatistics;
import com.hq.ims.data.entityenum.EnumPlatformTodoType;
import com.hq.ims.data.vo.PlatformTodoStatisticsDetailMapstruct;
import com.hq.ims.data.vo.PlatformTodoStatisticsMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 平台端待办统计 服务实现类
 * @since 20241108
 */
@Service
public class BasePlatformTodoStatisticsService extends BaseService<PlatformTodoStatisticsMapper, PlatformTodoStatistics> {

    @Resource
    private BasePlatformTodoStatisticsDetailService basePlatformTodoStatisticsDetailService;

    /**
     * 分页
     */
    public List<PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo> getByPage(
            RequestPage page,
            PlatformTodoStatisticsMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<PlatformTodoStatistics> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(PlatformTodoStatistics::getCreateDateTime);
        //查询条件

        //执行分页
        lq.page(page);

        List<PlatformTodoStatistics> dataList = page.getRecords();
        List<PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo> voList = PlatformTodoStatisticsMapstruct.INSTANCE.toPlatformTodoStatisticsVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    private PlatformTodoStatistics addData(PlatformTodoStatisticsMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        PlatformTodoStatistics entity = new PlatformTodoStatistics();
        entity.setPlatformTodoType(vo.getPlatformTodoType());
        entity.setPlatformTodoTypeCount(vo.getPlatformTodoTypeCount());
        entity.insert();
        return entity;
    }

    /**
     * 按类型累加数量,允许为负数
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCountByPlatformTodoType(
            EnumPlatformTodoType platformTodoType,
            Integer addCount,
            String dataId
    ) {
        PlatformTodoStatistics platformTodoStatistics = this.getByPlatformTodoType(platformTodoType);
        if (platformTodoStatistics == null) {
            PlatformTodoStatisticsMapstruct.SaveVo saveVo = new PlatformTodoStatisticsMapstruct.SaveVo();
            saveVo.setPlatformTodoType(platformTodoType);
            saveVo.setPlatformTodoTypeCount(addCount < 0 ? 0 : addCount);
            platformTodoStatistics = this.addData(saveVo);
        } else {
            int finalCount = platformTodoStatistics.getPlatformTodoTypeCount() + addCount;
            platformTodoStatistics.setPlatformTodoTypeCount(finalCount < 0 ? 0 : finalCount);
            platformTodoStatistics.updateById();
        }

        if (StringUtils.isNotEmpty(dataId)) {
            if (addCount > 0) {
                PlatformTodoStatisticsDetailMapstruct.SaveVo saveVo = new PlatformTodoStatisticsDetailMapstruct.SaveVo();
                saveVo.setPlatformTodoStatisticsId(platformTodoStatistics.getPlatformTodoStatisticsId());
                saveVo.setPlatformTodoType(platformTodoStatistics.getPlatformTodoType());
                saveVo.setTableId(dataId);
                basePlatformTodoStatisticsDetailService.addData(saveVo);
            } else if (addCount < 0) {
                basePlatformTodoStatisticsDetailService.delByPlatformTodoTypeAndTableId(platformTodoType, dataId);
            }
        }
    }

    /**
     * 根据平台待办类型获取统计
     *
     * @param platformTodoType 平台待办类型
     */
    public PlatformTodoStatistics getByPlatformTodoType(EnumPlatformTodoType platformTodoType) {
        List<PlatformTodoStatistics> list = this.lambdaQuery().eq(PlatformTodoStatistics::getPlatformTodoType, platformTodoType).list();
        if (ListUtils.isExistsEle(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取原始数据详情
     */
    public PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo getDetailVo(String platformTodoStatisticsId) {
        PlatformTodoStatistics entity = this.getById(platformTodoStatisticsId);
        PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo vo = PlatformTodoStatisticsMapstruct.INSTANCE.toPlatformTodoStatisticsVo(entity);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    public void clearTodo(PlatformTodoStatisticsMapstruct.ClearTodo clearTodo) {
        SpringBootValidateUtils.validate(clearTodo);

        List<PlatformTodoStatistics> list = this.lambdaQuery()
                .eq(PlatformTodoStatistics::getPlatformTodoType, clearTodo.getPlatformTodoType())
                .list();

        for (PlatformTodoStatistics platformTodoStatistics : list) {
            int diff = platformTodoStatistics.getPlatformTodoTypeCount() - clearTodo.getClearCount();
            diff = diff > 0 ? diff : 0;
            this.lambdaUpdate()
                    .eq(PlatformTodoStatistics::getPlatformTodoType, clearTodo.getPlatformTodoType())
                    .set(PlatformTodoStatistics::getPlatformTodoTypeCount, diff)
                    .update();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public List<PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo> getList() {
        List<PlatformTodoStatistics> list = this.lambdaQuery()
                .orderByAsc(PlatformTodoStatistics::getPlatformTodoType)
                .list();
        List<PlatformTodoStatisticsMapstruct.PlatformTodoStatisticsVo> voList = PlatformTodoStatisticsMapstruct.INSTANCE.toPlatformTodoStatisticsVoList(list);
        return voList;
    }
}